#include "mumzik_func.h"
/*#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "Mumzik.h"
#include "qsort.h"

using namespace std;*/

/* Reads data about mumziks from a file 
  Mumzik** mas_beg - pointer to pointer to the beginning of the array of mumziks
  Mumzik** mas_end - pointer to pointer to the element followin the last
  char* filename - data file name
*/
bool Read_Mumzik_from_file (Mumzik** mas_beg, Mumzik** mas_end, char* filename)
{
	size_t number_mum = 0;
	size_t number_basket = 0;
	size_t apples = 0;

	ifstream in (filename);
	if (!in)
		return 1;

	in>>number_mum;
	*mas_beg = new Mumzik [number_mum];
	*mas_end = *mas_beg + number_mum;
	for (size_t i = 0; i < number_mum; ++i)
	{
		in>>number_basket;
		(*mas_beg)[i].Change_num_basket(number_basket);
		for (size_t j = 0; j < number_basket; j++)
		{
			in>>apples;
			(*mas_beg)[i].Add_apples(apples);
		}
	}

	in.close();
	return 0;
}

/* Writes data about mumziks from a file
  Mumzik** mas_beg - pointer to pointer to the beginning of the array of mumziks
  Mumzik** mas_end - pointer to pointer to the element following the last
  char* filename - date file name
*/
bool Write_Mumzik_to_file (Mumzik* mas_beg, Mumzik* mas_end, char* filename)
{
	ofstream out(filename);
	if (!out)
		return 1;

	out<<(mas_end - mas_beg)<<endl;
	for (Mumzik *i = mas_beg; i != mas_end; ++i)
	{
		out<<( i->get_end() - i->get_beg() )<<endl;
		for (Basket *j = i->get_beg(); j != i->get_end(); ++j)
			out<<j->get_num()<<" ";
		out<<endl;
	}

	out.close();
	return 0;
}

/* Exchange apples between two mumziks
  Mumzik* first - pointer to the first mumzik
  Mumzik* second - pointer to the second mumzik
 */
size_t Exchange_apples (Mumzik* first, Mumzik* second, size_t num_bask_exch)
{
	size_t common_apples = 0;
	size_t apples_1 = 0;
	size_t apples_2 = 0;
	size_t i = 0;

	first->Res_cur_end();
	second->Res_cur_end();
	for (i = 0; i < num_bask_exch; ++i)
		apples_1 += first->Get_apples();
	for (i = 0; i < num_bask_exch; ++i)
		apples_2 += second->Get_apples();
	common_apples += apples_1%num_bask_exch;
	common_apples += apples_2%num_bask_exch;
	apples_1 = (size_t)(apples_1/num_bask_exch);
	apples_2 = (size_t)(apples_2/num_bask_exch);

	first->Res_cur_beg();
	second->Res_cur_beg();
	for (i = 0; i < num_bask_exch; ++i)
		second->Add_apples(apples_1);
	for (i = 0; i < num_bask_exch; ++i)
		first->Add_apples(apples_2);

	return common_apples;
}

/* Exchange apples between all mumziks
  Mumzik* left - pointer to the first element mumzik's array
  Mumzik* right - pointer to the element following the last mumzik's array
*/
size_t Global_exchange (Mumzik* left, Mumzik* right, size_t num_bask_exch)
{
	size_t common_apples = 0;

	if (left != right)
		--right;
	else
		return 0;

	while (left <= right)
	{
		common_apples += Exchange_apples (left, right, num_bask_exch);
		++left;
		--right;
	}

	return common_apples;
}